﻿/*
 * Copyright (C) 2012 TheLionsRoar <thelionwillalwaysroar@gmail.com>
 * Copyright (C) 2012 Jedrix <jedrix2454@gmail.com>
 * 
 * This file is part of the LEO Game Library.
 * 
 * LEO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * LEO is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have receieved a copy of the GNU General Public License
 * along with LEO. If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using Microsoft.Xna.Framework;

namespace LEO.Framework {
    public class Animation : Element {
        private Rectangle[] frames;
        private int frameWidth;
        private int frameHeight;
        private int framesPerSecond = 0;
        private int currentFrame = 0;
        private TimeSpan frameLength;
        private TimeSpan timer;

        public Rectangle[] Frames {
            get { return frames; }
        }

        public int FrameWidth {
            get { return frameWidth; }
        }

        public int FrameHeight {
            get { return frameHeight; }
        }

        public int FramesPerSecond {
            get { return framesPerSecond; }
            set {
                if(value < 1) {
                    framesPerSecond = 1;
                } else if(value > 60) {
                    framesPerSecond = 60;
                } else {
                    framesPerSecond = value;
                }
                frameLength = TimeSpan.FromSeconds(1 / (double)framesPerSecond);
            }
        }

        public int CurrentFrame {
            get { return currentFrame; }
        }

        public Rectangle CurrentFrameRect {
            get { return frames[CurrentFrame]; }
        }

        public Animation(int frameCount, int framewidth, int frameheight, int framespersecond, int xOffset = 0, int yOffset = 0) {
            frames = new Rectangle[frameCount];
            frameWidth = framewidth;
            frameHeight = frameheight;
            FramesPerSecond = framespersecond;

            for(int i = 0; i < frameCount; i++) {
                frames[i] = new Rectangle(xOffset + (frameWidth * i), yOffset, frameWidth, frameHeight);
            }
            Reset();
        }

        public override void Update(GameTime gameTime) {
            timer += gameTime.ElapsedGameTime;
            if(timer > frameLength) {
                timer = TimeSpan.Zero;
                currentFrame = (currentFrame + 1) % frames.Length;
            }

            base.Update(gameTime);
        }

        public void Reset() {
            currentFrame = 0;
            timer = TimeSpan.Zero;
        }
    }
}
